package saas.dev.clouduserprovide.service.impl;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import saas.dev.clouduserprovide.dto.UserDto;
import saas.dev.clouduserprovide.entity.User;
import saas.dev.clouduserprovide.event.OrderEvent;
import saas.dev.clouduserprovide.event.TestEvent;
import saas.dev.clouduserprovide.event.common.PublishEventUtil;
import saas.dev.clouduserprovide.function.UserDtoMapperFunction;
import saas.dev.clouduserprovide.service.UserTestService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @ClassName : UserTestServiceImpl
 * @Author : y1
 * @Date: 2021/4/29 00:06
 * @Description : 用户 提供接口测试类
 */
@Service
@Slf4j
public class UserTestServiceImpl implements UserTestService {
    @Value("${server.port}")
    private String port;
    @Autowired
    private PublishEventUtil publishEventUtil;
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;
    @Resource
    private UserDtoMapperFunction userDtoMapperFunction;
    /**
     * 测试接口
     *
     * @return
     */
    @Override
    public String userTest(HttpServletRequest request) {
//        AsyncContext asyncContext = request.startAsync();
//        asyncContext.complete();
        // update 2
//        applicationEventPublisher.publishEvent(new OrderEvent(null,"1"));

        List<ApplicationEvent> events = new ArrayList<>();
        //发送事件1
//        log.info("发送事件:TestEvent");
        Map<String,String> map = new HashMap<>(2);
        map.put("orderId","0001");
        map.put("status","2");
        events.add(new TestEvent(map, "修改订单",this));
//        publishEventUtil.sentEvent(new TestEvent(map, "修改订单"));
//        log.info("发送事件:TestEvent结束");
//        log.info("------------");
        //发送事件2
//        log.info("发送事件:OrderEvent");
        map = new HashMap<>(2);
        map.put("productId","A001");
        map.put("Inventory","-2");
        events.add(new OrderEvent(map, "修改库存"));
//        publishEventUtil.sentEvent(new OrderEvent(map, "修改库存"));

//        log.info("发送事件:OrderEvent结束");
        publishEventUtil.sentEvent(events);
        //???
        return "接口测试成功".concat(port);
    }

    /**
     * 查询单个对象
     *
     * @param id
     * @return
     */
    @Override
    public UserDto getUserDto(String id) {
        User user = new User();
        user.setId(id);
        user.setName("名称");
        user.setDeptId("1");
        List<User> users = CollUtil.newArrayList(user);
        UserDto opt = Optional.of(user).map(userDtoMapperFunction).orElseGet(UserDto::new);
        List<UserDto> userList = users.stream().map(userDtoMapperFunction).collect(Collectors.toList());
        return opt;
    }

}
